 var path = require('path');

 var fs = require('fs');

 var mergejs = require('merge-express-js');

 module.exports = function (UC, source, config, ArFor, cache, package) {


     var driveList = {};
     var router = function (req, res, next) {
         var _extend = JSON.stringify({});
         if (process.env.SDK) {

         } else {
             var filekey = (config._language || "") + '/' + package.id + (req.UserAgent.isMobile ? '/M/' : '/P/') + package.version + "." + (config.id + "").split('').join('.') + "." + config.version;

             var etag = config.getETag(filekey);

             if (req.isConditionalGET()) {
                 var _etag = req.getHeader('if-none-match');
                 if (etag == _etag && config.userKey != null) {

                     req.notModified()
                     return;
                 }
             }
             res.etag_key = etag;
         }

         var language = config.jlanguage.getvalue(config._language);
         language = language == 1 ? {} : language;
         var _language = {
             "direction": language.direction,
             "lang": language.lang,
             "List": language.List
         };
         if (_language.direction == null && _language.lang == 'ug') {
             _language.direction = 'rtl';
         }
         var drivefile = './safe-mode.js';
         _language = JSON.stringify(_language);

         if (req.params.type === "app") {
             drivefile = './app.js';
         } else if (req.params.type === "cordova") {
             drivefile = './cordova.js';
         } else if (req.params.type === "admin") {
             drivefile = './index.js';
         } else if (req.params.type === "istouch") {
             drivefile = './istouch.js';
         }

         var send = function (bytesRead, uuid) {
             if (config.userKey == null) {
                 if (uuid != null) {
                     bytesRead = bytesRead.Replace('_window.uuid', 'true');
                     bytesRead = bytesRead.Replace('<%uuid%>', uuid);
                     bytesRead = bytesRead.Replace('<%config.id%>', config.id + "");
                 } else {
                     var msLibrary = config.msLibrary();
                     msLibrary.sysinfo("uuid").then(function (code, obj) {

                         if (obj && obj.error == 0) {

                             uuid = obj.value;
                         } else {
                             uuid = 0;
                         }
                         send(bytesRead, uuid)
                     })

                     return;
                 }
             } else {

                 bytesRead = bytesRead.Replace('_window.uuid', 'false');
             }

             var domain = req.protocol + '://' + req.headers.host;


             bytesRead = bytesRead.Replace('"<%extend%>"', _extend);
             bytesRead = bytesRead.Replace('"<%language%>"', _language);
             bytesRead = bytesRead.Replace("<%domain%>", domain);
             bytesRead = bytesRead.Replace("<%version%>", package.version);
             res.Js(bytesRead);
         }
         if (!process.env.SDK&&driveList[drivefile] != null) {

             send(driveList[drivefile] + '')
         } else {

             var locafile = path.join(__dirname, './../drive/');
             mergejs.MergeNode({
                     uglifyjs: {
                         compress: {
                             drop_console: true
                         }
                     },
                     ProjectAddress: locafile,
                     module: false,
                     minjs: true,
                     RelativeAddress: drivefile
                 },
                 [],
                 function (code, error) {

                     driveList[drivefile] = code;
                     send(code + "")

                 });
         }

     }

     return router;
 }